{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7mGmQbAO5pQb"
      },
      "source": [
        "# 开始\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "添加jupyter中文语言包，运行后刷新页面，点击`settings->language->chinese`即可\n",
        "\n",
        "安装多线程下载工具aria2(可选)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install --upgrade pip\n",
        "%pip install jupyterlab-language-pack-zh-CN\n",
        "!apt update\n",
        "!apt install -y aria2\n",
        "!apt install -y git"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "安装字体(启智平台不需要修改，其他环境根据运行结果更改)\n",
        "- 先跳过这一步，直接运行下面的代码，如果出现字体下载慢，再回来安装字体"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!cp Arial.ttf /root/.config/Ultralytics"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "安装[依赖](https://openi.pcl.ac.cn/laborer/yolov5/src/branch/master/requirements.txt)并且检查PyTorch和GPU."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wbvMlHd_QwMG",
        "outputId": "e8225db4-e61d-4640-8b1f-8bfce3331cea"
      },
      "outputs": [],
      "source": [
        "%pip install -r requirements.txt  # install\n",
        "\n",
        "import torch\n",
        "print(torch.__version__)\n",
        "print(torch.cuda.is_available())\n",
        "print(torch.version.cuda)\n",
        "print(torch.backends.cudnn.enabled)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "测试`num_workers`最佳大小以获得最佳性能。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from time import time\n",
        "import multiprocessing as mp\n",
        "import torch\n",
        "import torchvision\n",
        "from torchvision import transforms\n",
        " \n",
        "transform = transforms.Compose([\n",
        "    torchvision.transforms.ToTensor(),\n",
        "    torchvision.transforms.Normalize((0.1307,), (0.3081,))\n",
        "])\n",
        " \n",
        "trainset = torchvision.datasets.MNIST(\n",
        "    root='../datasets/',\n",
        "    train=True,  #如果为True，从 training.pt 创建数据，否则从 test.pt 创建数据。\n",
        "    download=True, #如果为true，则从 Internet 下载数据集并将其放在根目录中。 如果已下载数据集，则不会再次下载。\n",
        "    transform=transform\n",
        ")\n",
        " \n",
        "print(f\"num of CPU: {mp.cpu_count()}\")\n",
        "for num_workers in range(2, mp.cpu_count(), 2):  \n",
        "    train_loader = torch.utils.data.DataLoader(trainset, shuffle=True, num_workers=num_workers, batch_size=64, pin_memory=True)\n",
        "    start = time()\n",
        "    for epoch in range(1, 3):\n",
        "        for i, data in enumerate(train_loader, 0):\n",
        "            pass\n",
        "    end = time()\n",
        "    print(\"Finish with:{} second, num_workers={}\".format(end - start, num_workers))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "修改`utils/dataloads.py`内204行`num_workers=nw`为以上最佳结果，启智cpu平台使用`num_workers=0`。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4JnkELT0cIJg"
      },
      "source": [
        "# 1. 检测\n",
        "\n",
        "'detect.py'在各种来源上运行YOLOv5推理，从[最新的YOLOv5版本](https://github.com/ultralytics/yolov5/releases)自动下载模型，并将结果保存到`runs/detect`。示例推理源是：\n",
        "\n",
        "```shell\n",
        "python detect.py --source 0  # webcam\n",
        "                          img.jpg  # image\n",
        "                          vid.mp4  # video\n",
        "                          screen  # screenshot\n",
        "                          path/  # directory\n",
        "                         'path/*.jpg'  # glob\n",
        "                         'https://youtu.be/LNwODJXcvt4'  # YouTube\n",
        "                         'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zR9ZbuQCH7FX",
        "outputId": "284ef04b-1596-412f-88f6-948828dd2b49"
      },
      "outputs": [],
      "source": [
        "!python detect.py --weights models/yolov5s.pt --img 640 --conf 0.25 --source data/images\n",
        "from IPython import display\n",
        "display.Image(filename='runs/detect/exp/zidane.jpg', width=600)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0eq1SMWl6Sfn"
      },
      "source": [
        "# 2. 验证\n",
        "\n",
        "在[COCO](https://cocodataset.org/#home)数据集的`val`或`test`拆分上验证模型的准确性。模型会从[最新的YOLOv5版本](https://github.com/ultralytics/yolov5/releases)自动下载。要按类显示结果，请使用`--verbose`标志。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WQPtK1QYVaD_",
        "outputId": "cf7d52f0-281c-4c96-a488-79f5908f8426"
      },
      "outputs": [],
      "source": [
        "# Download COCO val\n",
        "#torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')  # download (780M - 5000 images)#原始下载地址，可能下不动\n",
        "!aria2c -s 10 -x 10 -j 20 https://ultralytics.com/assets/coco2017val.zip#使用多线程下载\n",
        "!unzip -q coco2017val.zip -d datasets  # unzip\n",
        "#!rm coco2017val.zip #删除压缩包"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "下载失败可以加载已有数据集"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!cp /dataset/coco2017val.zip /code/datasetss/\n",
        "!unzip ../datasets/coco2017val.zip"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "X58w8JLpMnjH",
        "outputId": "3e234e05-ee8b-4ad1-b1a4-f6a55d5e4f3d"
      },
      "outputs": [],
      "source": [
        "# Validate YOLOv5s on COCO val\n",
        "!python val.py --weights models/yolov5s.pt --data coco.yaml --img 640 --half   #cpu运行去除--half参数"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZY2VXXXu74w5"
      },
      "source": [
        "# 3. 训练\n",
        "\n",
        "通过使用`roboflow`pip包从您的推理条件中抽样图像来关闭主动学习循环\n",
        "\n",
        "在[COCO128](https://www.kaggle.com/ultralytics/coco128)数据集上训练YOLOv5s模型，使用`--data coco128.yaml`，从预训练的`--weights yolov5s.pt`开始，或从随机初始化的`--weights '' --cfg yolov5s.yaml`开始\n",
        "\n",
        "- **[预训练模型](https://github.com/ultralytics/yolov5/tree/master/models)**已经从[latest YOLOv5 release](https://github.com/ultralytics/yolov5/releases)自动下载\n",
        "- **[数据集](https://github.com/ultralytics/yolov5/tree/master/data)**可供自动下载的包括: [COCO](https://github.com/ultralytics/yolov5/blob/master/data/coco.yaml), [COCO128](https://github.com/ultralytics/yolov5/blob/master/data/coco128.yaml), [VOC](https://github.com/ultralytics/yolov5/blob/master/data/VOC.yaml), [Argoverse](https://github.com/ultralytics/yolov5/blob/master/data/Argoverse.yaml), [VisDrone](https://github.com/ultralytics/yolov5/blob/master/data/VisDrone.yaml), [GlobalWheat](https://github.com/ultralytics/yolov5/blob/master/data/GlobalWheat2020.yaml), [xView](https://github.com/ultralytics/yolov5/blob/master/data/xView.yaml), [Objects365](https://github.com/ultralytics/yolov5/blob/master/data/Objects365.yaml), [SKU-110K](https://github.com/ultralytics/yolov5/blob/master/data/SKU-110K.yaml).\n",
        "- **训练结果**被保存在`runs/train/`，并且使用递增的运行目录，即`runs/train/exp2`, `runs/train/exp3`等\n",
        "<br>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!python train.py --weights models/yolov5s.pt --data data/coco128.yaml --epochs 30 --batch-size 8"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 4. 导出onnx模型\n",
        "\n",
        "- 一般onnx模型大约是pt模型的两倍大小\n",
        "- 部分参数说明(详细的请看export.py文件)\n",
        "    - weights: 模型权重文件路径,例如`yolov5s.pt`\n",
        "    - data: 数据集配置文件路径,例如`data/coco128.yaml`\n",
        "    - imgsz: 图片尺寸,例如`(640, 640)`\n",
        "    - device: 设备,例如`cpu`或`0 or 0,1,2,3`\n",
        "    - include: 导出格式,例如`onnx`\n",
        "    - half: 是否使用fp16半精度,默认`False`\n",
        "    - simplify: 是否简化模型,默认`False`\n",
        "    - opset: onnx版本,默认`12`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!python export.py --weights runs/train/exp/weights/best.pt --include onnx --opset=12 --simplify"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 在Roboflow上标记数据集（可选）\n",
        "[Roboflow](https://roboflow.com/?ref=ultralytics)使您能够使用自己的自定义数据轻松**组织、标记和准备**高质量的数据集。Roboflow还可以轻松建立主动学习管道，与您的团队协作改进数据集，并使用`roboflow`pip包直接集成到您的模型构建工作流中。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "i3oKtE4g-aNn"
      },
      "outputs": [],
      "source": [
        "#@title Select YOLOv5 🚀 logger {run: 'auto'}\n",
        "logger = 'Comet' #@param ['Comet', 'ClearML', 'TensorBoard']\n",
        "\n",
        "if logger == 'Comet':\n",
        "  %pip install -q comet_ml\n",
        "  import comet_ml; comet_ml.init()\n",
        "elif logger == 'ClearML':\n",
        "  %pip install -q clearml\n",
        "  import clearml; clearml.browser_login()\n",
        "elif logger == 'TensorBoard':\n",
        "  %load_ext tensorboard\n",
        "  %tensorboard --logdir runs/train"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1NcFxRcFdJ_O",
        "outputId": "bbeeea2b-04fc-4185-aa64-258690495b5a"
      },
      "outputs": [],
      "source": [
        "# Train YOLOv5s on COCO128 for 3 epochs\n",
        "!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "15glLzbQx5u0"
      },
      "source": [
        "# 4. 可视化"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nWOsI5wJR1o3"
      },
      "source": [
        "## Comet Logging and Visualization 🌟 NEW\n",
        "\n",
        "[Comet](https://www.comet.com/site/lp/yolov5-with-comet/?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=yolov5_colab) is now fully integrated with YOLOv5. Track and visualize model metrics in real time, save your hyperparameters, datasets, and model checkpoints, and visualize your model predictions with [Comet Custom Panels](https://www.comet.com/docs/v2/guides/comet-dashboard/code-panels/about-panels/?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=yolov5_colab)! Comet makes sure you never lose track of your work and makes it easy to share results and collaborate across teams of all sizes!\n",
        "\n",
        "Getting started is easy:\n",
        "```shell\n",
        "pip install comet_ml  # 1. install\n",
        "export COMET_API_KEY=<Your API Key>  # 2. paste API key\n",
        "python train.py --img 640 --epochs 3 --data coco128.yaml --weights yolov5s.pt  # 3. train\n",
        "```\n",
        "To learn more about all of the supported Comet features for this integration, check out the [Comet Tutorial](https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration). If you'd like to learn more about Comet, head over to our [documentation](https://www.comet.com/docs/v2/?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=yolov5_colab). Get started by trying out the Comet Colab Notebook:\n",
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1RG0WOQyxlDlo5Km8GogJpIEJlg_5lyYO?usp=sharing)\n",
        "\n",
        "<a href=\"https://bit.ly/yolov5-readme-comet2\">\n",
        "<img alt=\"Comet Dashboard\" src=\"https://user-images.githubusercontent.com/26833433/202851203-164e94e1-2238-46dd-91f8-de020e9d6b41.png\" width=\"1280\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Lay2WsTjNJzP"
      },
      "source": [
        "## ClearML Logging and Automation 🌟 NEW\n",
        "\n",
        "[ClearML](https://cutt.ly/yolov5-notebook-clearml) is completely integrated into YOLOv5 to track your experimentation, manage dataset versions and even remotely execute training runs. To enable ClearML (check cells above):\n",
        "\n",
        "- `pip install clearml`\n",
        "- run `clearml-init` to connect to a ClearML server (**deploy your own [open-source server](https://github.com/allegroai/clearml-server)**, or use our [free hosted server](https://cutt.ly/yolov5-notebook-clearml))\n",
        "\n",
        "You'll get all the great expected features from an experiment manager: live updates, model upload, experiment comparison etc. but ClearML also tracks uncommitted changes and installed packages for example. Thanks to that ClearML Tasks (which is what we call experiments) are also reproducible on different machines! With only 1 extra line, we can schedule a YOLOv5 training task on a queue to be executed by any number of ClearML Agents (workers).\n",
        "\n",
        "You can use ClearML Data to version your dataset and then pass it to YOLOv5 simply using its unique ID. This will help you keep track of your data without adding extra hassle. Explore the [ClearML Tutorial](https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration) for details!\n",
        "\n",
        "<a href=\"https://cutt.ly/yolov5-notebook-clearml\">\n",
        "<img alt=\"ClearML Experiment Management UI\" src=\"https://github.com/thepycoder/clearml_screenshots/raw/main/scalars.jpg\" width=\"1280\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-WPvRbS5Swl6"
      },
      "source": [
        "## Local Logging\n",
        "\n",
        "Training results are automatically logged with [Tensorboard](https://www.tensorflow.org/tensorboard) and [CSV](https://github.com/ultralytics/yolov5/pull/4148) loggers to `runs/train`, with a new experiment directory created for each new training as `runs/train/exp2`, `runs/train/exp3`, etc.\n",
        "\n",
        "This directory contains train and val statistics, mosaics, labels, predictions and augmentated mosaics, as well as metrics and charts including precision-recall (PR) curves and confusion matrices.\n",
        "\n",
        "<img alt=\"Local logging results\" src=\"https://user-images.githubusercontent.com/26833433/183222430-e1abd1b7-782c-4cde-b04d-ad52926bf818.jpg\" width=\"1280\"/>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Zelyeqbyt3GD"
      },
      "source": [
        "# Environments\n",
        "\n",
        "YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including [CUDA](https://developer.nvidia.com/cuda)/[CUDNN](https://developer.nvidia.com/cudnn), [Python](https://www.python.org/) and [PyTorch](https://pytorch.org/) preinstalled):\n",
        "\n",
        "- **Notebooks** with free GPU: <a href=\"https://bit.ly/yolov5-paperspace-notebook\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"></a> <a href=\"https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a> <a href=\"https://www.kaggle.com/ultralytics/yolov5\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\n",
        "- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/)\n",
        "- **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/)\n",
        "- **Docker Image**. See [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/) <a href=\"https://hub.docker.com/r/ultralytics/yolov5\"><img src=\"https://img.shields.io/docker/pulls/ultralytics/yolov5?logo=docker\" alt=\"Docker Pulls\"></a>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6Qu7Iesl0p54"
      },
      "source": [
        "# Status\n",
        "\n",
        "![YOLOv5 CI](https://github.com/ultralytics/yolov5/actions/workflows/ci-testing.yml/badge.svg)\n",
        "\n",
        "If this badge is green, all [YOLOv5 GitHub Actions](https://github.com/ultralytics/yolov5/actions) Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training ([train.py](https://github.com/ultralytics/yolov5/blob/master/train.py)), testing ([val.py](https://github.com/ultralytics/yolov5/blob/master/val.py)), inference ([detect.py](https://github.com/ultralytics/yolov5/blob/master/detect.py)) and export ([export.py](https://github.com/ultralytics/yolov5/blob/master/export.py)) on macOS, Windows, and Ubuntu every 24 hours and on every commit.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IEijrePND_2I"
      },
      "source": [
        "# Appendix\n",
        "\n",
        "Additional content below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GMusP4OAxFu6"
      },
      "outputs": [],
      "source": [
        "# YOLOv5 PyTorch HUB Inference (DetectionModels only)\n",
        "import torch\n",
        "\n",
        "model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True, trust_repo=True)  # or yolov5n - yolov5x6 or custom\n",
        "im = 'https://ultralytics.com/images/zidane.jpg'  # file, Path, PIL.Image, OpenCV, nparray, list\n",
        "results = model(im)  # inference\n",
        "results.print()  # or .show(), .save(), .crop(), .pandas(), etc."
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "YOLOv5 Tutorial",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
